from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep

import sys

sys.path.append(r'..')
from web_base.base_page import BasePage
from page_object.login_page import LoginPage
from test_case.options import options


class AssetPage(BasePage):
    url = r'/bsams/front/asset/asset_list.do'
    # 新增定位
    add_btn_loc = (By.CLASS_NAME, 'button_true')
    add_title_loc = (By.ID, 'title')
    add_providerId_loc = (By.ID, 'providerId')
    add_storageId_loc = (By.ID, 'storageId')
    add_ok_loc = (By.CLASS_NAME, 'button_fujian.margin')

    # 修改
    mod_btn_loc = (By.LINK_TEXT, '休改')

    mod_title_loc = (By.ID, 'title')
    mod_providerId_loc = (By.ID, 'providerId')
    mod_storageId_loc = (By.ID, 'storageId')
    mod_ok_loc = (By.CLASS_NAME, 'button_fujian.margin')
    # 查询
    sel_status_loc = (By.ID, 'status')
    sel_title_loc = (By.ID, 'title')
    sel_ok_loc = (By.CLASS_NAME, 'search_button')
    # 借用
    borrow_btn_loc = (By.LINK_TEXT, '借用')
    borrow_departId_loc = (By.ID, 'departId')
    borrow_remark_loc = (By.ID, 'remark')
    borrow_ok_loc = (By.XPATH, '//*[@id="fm_new"]/div/div[1]')
    # 归还
    return_btn_loc=(By.CLASS_NAME,'padding5')
    return_sure_loc=(By.XPATH, '//*[@id="cboxLoadedContent"]/div/div/div[2]/div[2]/div[1]')


    def asset_add(self, title, providerId, storageId):
        self.open()
        self.ele_click(self.add_btn_loc)
        self.input_ele(self.add_title_loc, title)
        self.ele_select(self.add_providerId_loc, providerId)
        self.ele_select(self.add_storageId_loc, storageId)
        self.ele_click(self.add_ok_loc)
        sleep(3)
        text = self.get_alert_text()
        self.alert_click()
        return text

    # 修改
    def asset_mod(self, title, providerId, storageId):
        self.open()
        self.ele_click(self.mod_btn_loc)
        self.ele_clear(self.mod_title_loc)
        self.input_ele(self.mod_title_loc, title)
        self.ele_select(self.mod_providerId_loc, providerId)
        self.ele_select(self.mod_storageId_loc, storageId)
        self.ele_click(self.mod_ok_loc)
        sleep(3)
        text = self.get_alert_text()
        self.alert_click()
        return text

    # 查询
    def asset_select(self, status, title):
        self.open()
        self.ele_select(self.sel_status_loc, status)
        self.ele_clear(self.sel_title_loc)
        self.inpit_ele(self.sel_title_loc, title)
        self.ele_click(self.sel_ok_loc)
        sleep(3)

    def asset_borrow(self, departId, remark):
        self.open()
        self.ele_click(self.borrow_btn_loc)
        self.ele_select(self.borrow_departId_loc,departId)
        self.ele_clear(self.borrow_remark_loc)
        self.input_ele(self.borrow_remark_loc, remark)
        self.ele_click(self.borrow_btn_loc)
        sleep(3)
        text = self.get_alert_text()
        self.alert_click()
        return text

    def asset_return(self):
        self.open()
        self.ele_click(self.return_btn_loc)
        self.ele_click(self.return_sure_loc)
        sleep(3)
        text = self.get_alert_text()
        self.alert_click()
        return text


if __name__ == '__main__':
    title = '联想小新01'
    providerId = '维信科技发展有限公司'
    storageId = '电脑设备库'

    driver = webdriver.Chrome(options=options)

    login_page = LoginPage(driver)
    login_page.login()

    asset_page = AssetPage(driver)
    text = asset_page.asset_add(title, providerId, storageId)
    print(text)

    driver.quit()
